/**
 * 文 件 名:  ContentController.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.base.utils.WebUtils;
import com.czd.support.common.Configuration;
import com.czd.support.model.Content;
import com.czd.support.model.Manager;
import com.czd.support.service.ContentService;

/**
 * @Description  内容控制器
 * 
 * @Author  Jun.Yang
 * @Date    2015年3月16日 上午11:12:46
 * @Version v1.0
 */
@Controller
@RequestMapping("/content")
public class ContentController extends BaseController {
	@Autowired
	private ContentService contentService;
	/**
	 * 内容信息列表 
	 * @param content
	 * @param request
	 * @param model
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"/", "", "list"}, method = RequestMethod.GET)
	public String list(Content content, HttpServletRequest request, Model model, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		Manager manager = this.getSesionUser(request);
		if(manager != null){
			content.setPageSize(Configuration.getInstance().getDefault_page_size());
			content.setPageNo(pageNo);
			String kw = WebUtils.findParamStr(request,"kw");
			System.out.println(kw);
			Paginate paginate = contentService.queryForPage(content , kw);
			model.addAttribute("paginate", paginate);
			return "content/list";
		}else{
			return "redirect:/login";
		}
		
	}
	
	
	/**
	 * 跳转到内容信息录入页面
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return "content/add";
	}
	
	/**
	 * 内容信息录入
	 * @param request
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, Content content, Model model) {
		Manager manager = this.getSesionUser(request);
		content.setCreateUser(manager.getUserCode());
		if(StringUtils.isNotBlank(content.getTitleImg())) {
			content.setHasTitleImg(1);
		}else {
			content.setHasTitleImg(0);
		}
		if(contentService.insert(content)) {
			return "redirect:/content/"+content.getId()+"/details";
		}
		
		return error(model, "操作失败", "录入内容信息失败");
	}
	
	/**
	 * 跳转到内容信息编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		Content content = contentService.findById(id);
		System.out.println(content);
		if(content!=null) {
			model.addAttribute("content", content);
			return "content/edit";
		}
		
		return error(model, "内容信息不存在", "内容信息不存在");
	}
	
	/**
	 * 更新内容信息
	 * @param request
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST) 
	public String edit(HttpServletRequest request, Content content, Model model){
		Manager manager = this.getSesionUser(request);
		content.setUpdateUser(manager.getUserCode());
		if(contentService.update(content)) {
			return "redirect:/content/"+content.getId()+"/details";
		}
		
		return  error(model, "操作失败", "更新内容信息失败");
	}

	/**
	 * 删除内容信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable(value="id") int id, Model model) {
		if(contentService.delete(id)) {
			return "redirect:/content/list";
		}
		
		return error(model, "操作失败", "删除内容信息失败");
	}
	
	/**
	 * 内容信息详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		Content content = contentService.findById(id);
		if(content!=null) {
			model.addAttribute("content", content);
			return "content/details";
		}
		return error(model, "内容信息不存在", "内容信息不存在");
	}
}
